package com.qfedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qfedu.entity.Song;
import com.qfedu.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
public class SongController {
    @Autowired
    private SongService songService;
    // 返回所有歌曲
    @RequestMapping("/song")
    @Cacheable(value = "song")
    public List<Song> findAllSong(){
        List<Song> songs = songService.findAllSong();
        return songs;
    }
    // 返回指定歌手ID的歌曲
    @RequestMapping("/song/singer/detail")
    public List<Song> findSongBySingerId(int singerId){
        List<Song> songBySingerId = songService.findSongBySingerId(singerId);
        return songBySingerId;
    }
    // 返回的指定用户ID收藏列表
    @RequestMapping("/song/detail")
    public List<Song> findSongById(int id){
        System.out.println("id = " + id);
        List<Song> songById = songService.findSongById(id);
        return songById;
    }
    // 返回指定歌手名的歌曲
    @RequestMapping("/song/singerName/detail")
    public List<Song> findSongByName(String name){
        List<Song> songByName = songService.findSongByName(name);
        return songByName;
    }
    // 更新歌曲信息
    @RequestMapping("/song/update")
    public int updateSong(Song song){
        int i= songService.updateSong(song);
        return i;
    }

    // 删除歌曲
    @RequestMapping("/song/delete")
    public int deleteSong(int id){
        int i = songService.deleteSong(id);
        return i;
    }

    //上传图片
    @RequestMapping("/song/img/update")
    public JSONObject updatePic(MultipartFile file, @RequestParam(value = "id") int id) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/img/songPic/" + originalFilename;//originalFilename 文件名
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\img\\songPic\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file1 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file1);

        Song song = new Song();
        song.setId(id);
        song.setPic(url);
        System.out.println("song = " + song);
        JSONObject jsonObject = songService.updatePic(song);
        jsonObject.put("pic",url);
        return jsonObject;

    }

    //上传歌曲
    @RequestMapping("/song/add")
    public JSONObject addUrl(MultipartFile file,Song song) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/song/" + originalFilename;
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\song\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file2 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file2);
        Date date = new Date();
        song.setCreateTime(date);
        song.setUpdateTime(date);
        song.setUrl(url);
        JSONObject jsonObject = songService.addUrl(song);
        jsonObject.put("url",url);
        return jsonObject;

    }

    @RequestMapping("/song/url/update")
    public JSONObject updateSongById(MultipartFile file,Song song) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/song/" + originalFilename;
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\song\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file3 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file3);

        song.setUrl(url);
        JSONObject jsonObject = songService.updateSongById(song);
        jsonObject.put("url",url);
        return jsonObject;
    }


}
